clear all

*******

global InputDataDir = "...\data" // set the directory for the input data file
global OutputDir = "..\results" // set the directory for output files
global DataFile = "factor_regs"
***********

cd "$InputDataDir"


use $factor_regs

cd "$OutputDir"

**********************
** TABLE 4
**********************

* Carhart 4-factor
 newey excess_ret_rf_EEW MktRF SMB HML MOM, lag(1)
 outreg2 using Table4, replace dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) label excel	
 
 * FF 6-factor
 newey excess_ret_rf_EEW MktRF SMB_5 HML MOM RMW CMA, lag(1)
 outreg2 using Table4, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	

 * q
 newey excess_ret_rf_EEW R_MKT R_ME R_IA R_ROE, lag(1)
 outreg2 using Table4, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	
 
 * q5
 newey excess_ret_rf_EEW R_MKT R_ME R_IA R_ROE R_EG, lag(1)
 outreg2 using Table4, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	
 
 * AQR
 newey excess_ret_rf_EEW MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
 outreg2 using Table4, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	
 

**********************
** TABLE 5
**********************

 * Carhart 4-factor
newey excess_ret_rf_EEW MktRF SMB HML MOM, lag(1)
outreg2 using Table5, replace dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel	

newey excess_ret_rf_EW_ex50 MktRF SMB HML MOM, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EW_ex50) label excel	

newey excess_ret_rf_VW MktRF SMB HML MOM, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_VW) label excel

newey excess_ret_rf_EVW_cap90 MktRF SMB HML MOM, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap90) label excel

newey excess_ret_rf_EVW_cap80 MktRF SMB HML MOM, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap80) label excel

 * FF 6
newey excess_ret_rf_EEW MktRF SMB HML MOM RMW CMA, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel	

newey excess_ret_rf_EW_ex50 MktRF SMB HML MOM RMW CMA, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EW_ex50) label excel	

newey excess_ret_rf_VW MktRF SMB HML MOM RMW CMA, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_VW) label excel

newey excess_ret_rf_EVW_cap90 MktRF SMB HML MOM RMW CMA, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap90) label excel

newey excess_ret_rf_EVW_cap80 MktRF SMB HML MOM RMW CMA, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap80) label excel

 * q
newey excess_ret_rf_EEW R_MKT R_ME R_IA R_ROE, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel	

newey excess_ret_rf_EW_ex50 R_MKT R_ME R_IA R_ROE, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EW_ex50) label excel	

newey excess_ret_rf_VW R_MKT R_ME R_IA R_ROE, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_VW) label excel

newey excess_ret_rf_EVW_cap90 R_MKT R_ME R_IA R_ROE, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap90) label excel

newey excess_ret_rf_EVW_cap80 R_MKT R_ME R_IA R_ROE, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap80) label excel

 * q5
newey excess_ret_rf_EW R_MKT R_ME R_IA R_ROE R_EG, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EW) label excel	

newey excess_ret_rf_EW_ex50 R_MKT R_ME R_IA R_ROE R_EG, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EW_ex50) label excel	

newey excess_ret_rf_VW R_MKT R_ME R_IA R_ROE R_EG, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_VW) label excel

newey excess_ret_rf_EVW_cap90 R_MKT R_ME R_IA R_ROE R_EG, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap90) label excel

newey excess_ret_rf_EVW_cap80 R_MKT R_ME R_IA R_ROE R_EG, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap80) label excel

 * AQR
newey excess_ret_rf_EEW MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel	

newey excess_ret_rf_EW_ex50 MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EW_ex50) label excel	

newey excess_ret_rf_VW MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_VW) label excel

newey excess_ret_rf_EVW_cap90 MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap90) label excel

newey excess_ret_rf_EVW_cap80 MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
outreg2 using Table5, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EVW_cap80) label excel


**********************
** TABLE 7
**********************

 * FF 6-factor with HML_INT
 newey excess_ret_rf_EEW MktRF SMB_5 hml_int_t100 MOM RMW CMA, lag(1)
 outreg2 using Table7, replace dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel
 
 * FF 6-factor with HML_IME
 newey excess_ret_rf_EEW MktRF SMB_5 hml_ime_t100 MOM RMW CMA, lag(1)
 outreg2 using Table7, append dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel

  * FF 6-factor with PTINT
 newey excess_ret_rf_EEW MktRF SMB_5 hml_int_t30 MOM RMW CMA, lag(1)
 outreg2 using Table7, append dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel
 
   ** HML_Int augmented q5
 * q5 with HML_INT
 newey excess_ret_rf_EW R_MKT R_ME R_IA R_ROE R_EG hml_int_t100, lag(1)
 outreg2 using Table7, append dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel
 
  * q5 with HML_IME
 newey excess_ret_rf_EW R_MKT R_ME R_IA R_ROE R_EG hml_ime_t100, lag(1)
 outreg2 using Table7, append dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel
 
  * q5 with HML_PTINT
 newey excess_ret_rf_EW R_MKT R_ME R_IA R_ROE R_EG hml_int_t30, lag(1)
 outreg2 using Table7, append dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel

 ** ZEKHNINI et al. 
  newey excess_ret_rf_EEW Market SizeTotal HMLTotal MOM RMWTotal CMATotalPPE , lag(1)
 outreg2 using Table7, append dec(2)  stats(coef tstat) sortvar(Constant) drop(`excess_ret') label excel
 
 **********************
** TABLE 8 
**********************
 * Carhart 4-factor

newey excess_ret_rf_EEW MktRF SMB HML MOM MktRF, lag(1)
outreg2 using Table8, replace dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel	
 
 newey excess_ret_rf_EEW MktRF SMB HML MOM MktRF BC_EW_alpha, lag(1)
outreg2 using Table8, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel	
	
 * FF 6-factor
 newey excess_ret_rf_EEW MktRF SMB_5 HML MOM RMW CMA , lag(1)
outreg2 using Table8, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel
 
newey excess_ret_rf_EEW MktRF SMB_5 HML MOM RMW CMA BC_EW_alpha, lag(1)
outreg2 using Table8, append dec(2)  stats(coef tstat) sortvar(Constant) drop(excess_ret_rf_EEW) label excel

*******************************************


 **********************
** Figure 5
**********************

* Carhart 4-factor
 newey excess_ret_rf_EEW_NoRebal2001 MktRF SMB HML MOM, lag(1)
 outreg2 using Figure5, replace dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) label excel	
 
 * FF 6-factor
 newey excess_ret_rf_EEW_NoRebal2001 MktRF SMB_5 HML MOM RMW CMA, lag(1)
 outreg2 using Figure5, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	

 * q
 newey excess_ret_rf_EEW_NoRebal2001 R_MKT R_ME R_IA R_ROE, lag(1)
 outreg2 using Figure5, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	
 
 * q5
 newey excess_ret_rf_EEW_NoRebal2001 R_MKT R_ME R_IA R_ROE R_EG, lag(1)
 outreg2 using Figure5, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	
 
 * AQR
 newey excess_ret_rf_EEW_NoRebal2001 MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
 outreg2 using Figure5, dec(2)  stats(coef tstat) sortvar(_cons) drop(excess_ret_rf_EEW) append label excel	
 
****************************************************************************
**FIGURE 4
***********************************************************
 

************
* Carhart
************
newey excess_ret_rf_EEW MktRF SMB HML MOM, lag(1)
predict residuals_carhart, residuals
gen alpha_plus_resid = _b[_cons] + residuals_carhart

* Step 1: Convert monthly returns to compounding factors
gen monthly_factor = 1 + alpha_plus_resid / 100

* Step 2: Calculate cumulative returns using a loop
gen cumul_return = 1
replace cumul_return = monthly_factor if _n == 1
replace cumul_return = cumul_return[_n-1] * monthly_factor if _n > 1

* Step 3: Convert cumulative return to percentage
gen cumulative_percent_carhart = (cumul_return - 1) * 100
twoway line cumulative_percent_carhart ymdate
drop cumul_return monthly_factor  
drop alpha_plus_resid
************
* FF6
************
newey excess_ret_rf_EEW MktRF SMB HML MOM RMW CMA, lag(1)
predict residuals_ff6, residuals
gen alpha_plus_resid = _b[_cons] + residuals_ff6

* Step 1: Convert monthly returns to compounding factors
gen monthly_factor = 1 + alpha_plus_resid / 100

* Step 2: Calculate cumulative returns using a loop
gen cumul_return = 1
replace cumul_return = monthly_factor if _n == 1
replace cumul_return = cumul_return[_n-1] * monthly_factor if _n > 1

* Step 3: Convert cumulative return to percentage
gen cumulative_percent_ff6 = (cumul_return - 1) * 100
twoway line cumulative_percent_ff6 ymdate
drop cumul_return monthly_factor 
drop alpha_plus_resid
************
* q
************
newey excess_ret_rf_EEW R_MKT R_ME R_IA R_ROE, lag(1)
predict residuals_q, residuals
gen alpha_plus_resid = _b[_cons] + residuals_q

* Step 1: Convert monthly returns to compounding factors
gen monthly_factor = 1 + alpha_plus_resid / 100

* Step 2: Calculate cumulative returns using a loop
gen cumul_return = 1
replace cumul_return = monthly_factor if _n == 1
replace cumul_return = cumul_return[_n-1] * monthly_factor if _n > 1

* Step 3: Convert cumulative return to percentage
gen cumulative_percent_q = (cumul_return - 1) * 100
twoway line cumulative_percent_q ymdate
drop cumul_return monthly_factor 
drop alpha_plus_resid

************
* q5
************
newey excess_ret_rf_EW R_MKT R_ME R_IA R_ROE R_EG, lag(1)
predict residuals_q5, residuals
gen alpha_plus_resid = _b[_cons] + residuals_q5

* Step 1: Convert monthly returns to compounding factors
gen monthly_factor = 1 + alpha_plus_resid / 100

* Step 2: Calculate cumulative returns using a loop
gen cumul_return = 1
replace cumul_return = monthly_factor if _n == 1
replace cumul_return = cumul_return[_n-1] * monthly_factor if _n > 1

* Step 3: Convert cumulative return to percentage
gen cumulative_percent_q5 = (cumul_return - 1) * 100
twoway line cumulative_percent_q5 ymdate
drop cumul_return monthly_factor 
drop alpha_plus_resid

************
* AQR
************
newey excess_ret_rf_EEW MKT SMB_AQR HML_AQR UMD BAB QMJ, lag(1)
predict residuals_aqr, residuals
gen alpha_plus_resid = _b[_cons] + residuals_aqr

* Step 1: Convert monthly returns to compounding factors
gen monthly_factor = 1 + alpha_plus_resid / 100

* Step 2: Calculate cumulative returns using a loop
gen cumul_return = 1
replace cumul_return = monthly_factor if _n == 1
replace cumul_return = cumul_return[_n-1] * monthly_factor if _n > 1

* Step 3: Convert cumulative return to percentage
gen cumulative_percent_aqr = (cumul_return - 1) * 100
twoway line cumulative_percent_aqr ymdate
drop cumul_return monthly_factor 
drop alpha_plus_resid

*******************************
** set the month before the start to be 0
* Add a new observation
set obs `=_N+1'

* Convert "2000m8" to STATA's numeric date format (months since January 1960)
gen newdate = ym(2000, 8)

* Assign the numeric date to the new observation
replace ymdate = newdate if ymdate == .

* Optionally, to confirm that ymdate is handled as a date, format it
format ymdate %tm
sort ymdate

foreach cum_ret in cumulative_percent_carhart cumulative_percent_ff6 cumulative_percent_q cumulative_percent_q5 cumulative_percent_aqr {
	replace `cum_ret' = 0 if `cum_ret' == .
}

label var cumulative_percent_carhart  "Carhart"
label var cumulative_percent_ff6  "FF6"
label var cumulative_percent_q  "q"
label var cumulative_percent_q5  "q5"
label var cumulative_percent_aqr  "AQR"
********************
** Graph all together
********************
twoway (line  cumulative_percent_carhart ymdate, lpattern(solid) lwidth(0.3) lcolor(black)) ///
(line  cumulative_percent_ff6 ymdate, lpattern(dash) lwidth(0.3) lcolor(red)) ///
(line  cumulative_percent_q ymdate, lpattern(longdash) lwidth(0.3) lcolor(blue)) ///
(line  cumulative_percent_q5 ymdate, lpattern(longdash_dot) lwidth(0.3) lcolor(brown)) ///
(line  cumulative_percent_aqr ymdate, lpattern(shortdash_dot) lwidth(0.3) lcolor(green))

format ymdate %tm
xtset ymdate

twoway (connected cumulative_percent_carhart ymdate, msymbol(O) msize(vsmall) lwidth(0.1) lcolor(black) mcolor(black)) ///
       (connected cumulative_percent_ff6 ymdate, msymbol(s) msize(vsmall) lwidth(0.1) lcolor(red) mcolor(red)) ///
       (connected cumulative_percent_q ymdate, msymbol(+) msize(vsmall) lwidth(0.1) lcolor(blue) mcolor(blue)) ///
       (connected cumulative_percent_q5 ymdate, msymbol(th) msize(vsmall) lwidth(0.1) lcolor(brown) mcolor(brown)) ///
       (connected cumulative_percent_aqr ymdate, msymbol(v) msize(vsmall) lwidth(0.1) lcolor(green) mcolor(green)), ///
	   xlabel(, labsize(small)) ///
	    xlabel(, format(%tmCY) valuelabel) ///
	   ylabel(, labsize(small)) ///
	   ytitle("Cumulative excess return (%)", size(small)) ///
	   xtitle("Date", size(small)) ///
       legend( order(1 "Carhart" 2 "FF6" 3 "q" 4 "q5" 5 "AQR") region(lc(none) fc(none)) position(5) ring(0) margin(0 0 0 0) rows(1) size(small))
graph export Figure4.png, replace
graph export Figure4.jpg, replace

